import sys
from model.Config import Config
from model.OneScoreRank import OneScoreRank
from model.OneScoreRankShengNei import OneScoreRankShengNei
from model.University import University
from model.UniversityMajorRankMean import UniversityMajorRankMean
from model.UniversityScoreRank import UniversityScoreRank
from model.UniversityScoreRankMean import UniversityScoreRankMean


# 以university_major_plan(招生计划)数据做参照
def mean_by_major():
    universityMajorRankMean = UniversityMajorRankMean()
    universityScoreRankMean = UniversityScoreRankMean()
    universityScoreRank = UniversityScoreRank()
    year = 2023
    mean_year = 2020  # 获取三年平均数时候的起始年份
    mean_year_2 = 2022  # 获取三年平均数时候的起始年份
    # batch_list = [9, 11, 13, 14, 19]
    batch_list = [19]
    type_list = [1, 2]
    total_list = list()
    for i in batch_list:
        for j in type_list:
            data = universityMajorRankMean.getListByGroup(year)

            for index, row in enumerate(data):
                # total_list = list()
                row_1 = universityMajorRankMean.getOneByYearBatchType(year, i, j, row['code'])
                if row_1:
                    # 院校排名
                    university = University()
                    university_row = university.getOneByCode(row['code'])
                    if university_row:
                        row_1['university_rank'] = university_row['university_rank']
                        row_1['type_name'] = university_row['type_name']
                        row_1['dual_class_name'] = university_row['dual_class_name']
                        row_1['dual_class_A'] = university_row['dual_class_A']
                        row_1['dual_class_B'] = university_row['dual_class_B']
                        row_1['dual_class_subject'] = university_row['dual_class_subject']
                        row_1['f985'] = university_row['f985']
                        row_1['f211'] = university_row['f211']
                        row_1['nature_name'] = university_row['nature_name']
                        row_1['other_type'] = university_row['other_type']
                        row_1['change_difficult_level'] = university_row['change_difficult_level']
                    else:
                        row_1['university_rank'] = 999  # 默认设置院校排名为999
                        row_1['dual_class_name'] = ''
                        row_1['dual_class_A'] = 0
                        row_1['dual_class_B'] = 0
                        row_1['dual_class_subject'] = 0
                        row_1['f985'] = 0
                        row_1['f211'] = 0
                        row_1['type_name'] = ''
                        row_1['nature_name'] = 0
                        row_1['other_type'] = 0
                        row_1['change_difficult_level'] = ''
                    # 近三年录取最低分、录取最低排名、录取平均分、录取平均排名
                    data_4 = universityScoreRank.getRowsByYearBatch(mean_year, mean_year_2, row_1['batch'],
                                                                    row_1['type'], row_1['code'])

                    # 计算专业近三年录取平均分的平均值、近三年录取最低分平均值、近三年录取最低排名平均值、近三年录取平均排名平均值
                    row_1['mean_lowest_score'] = mean_lowest_score(data_4)
                    row_1['mean_lowest_rank'] = mean_lowest_rank(data_4)
                    row_1['mean_mean_score'] = mean_mean_score(data_4)
                    row_1['mean_mean_rank'] = mean_mean_rank(data_4)
                    row_1['assign_mean_rank'] = assign_mean_rank(data_4)
                    row_1['assign_lowest_rank'] = assign_lowest_rank(data_4, row_1['type'])

                    # 取最新一年的《高考指南》的投档数据
                    one = universityScoreRank.getOneByYearBatch(2022, row_1['batch'], row_1['type'], row_1['code'])
                    if one:
                        row_1['plan_number'] = one['plan_number']
                        row_1['submit_number'] = one['submit_number']
                        row_1['enter_number'] = one['enter_number']
                    else:
                        row_1['plan_number'] = 0
                        row_1['submit_number'] = 0
                        row_1['enter_number'] = 0

                    row_1['name'] = row_1['uname']
                    del row_1['uname']
                    print('row_1', row_1)
                    total_list.append(row_1)

                # if index == 10:
                #     print(total_list)
                #     universityScoreRankMean.toInsertBatch(total_list)
                #     sys.exit()
    # print(total_list)
    universityScoreRankMean.toInsertBatch(total_list)


# 计算专业近三年录取最低分的平均值
def mean_lowest_score(data):
    i = 0
    total_rank = 0
    for index, row in enumerate(data):
        if index < 3:
            if row['lowest_score'] > 0:
                i = i + 1
            total_rank = total_rank + row['lowest_score']
    mean_rank = 0
    if i > 0:
        mean_rank = round(total_rank / i)
    return mean_rank


# 计算专业近三年录取最低分的排名的平均值
def mean_lowest_rank(data):
    i = 0
    total_rank = 0
    for index, row in enumerate(data):
        if index < 3:
            if row['lowest_rank'] > 0:
                i = i + 1
            total_rank = total_rank + row['lowest_rank']
    mean_rank = 0
    if i > 0:
        mean_rank = round(total_rank / i)
    return mean_rank


# 计算专业近三年录取平均分的平均值
def mean_mean_score(data):
    i = 0
    total_rank = 0
    for index, row in enumerate(data):
        if index < 3:
            if row['mean_score'] > 0:
                i = i + 1
            total_rank = total_rank + row['mean_score']
    mean_rank = 0
    if i > 0:
        mean_rank = round(total_rank / i)
    return mean_rank


# 计算专业近三年录取平均排名的平均值
def mean_mean_rank(data):
    i = 0
    total_rank = 0
    for index, row in enumerate(data):
        if index < 3:
            if row['mean_rank'] > 0:
                i = i + 1
            total_rank = total_rank + row['mean_rank']
    mean_rank = 0
    if i > 0:
        mean_rank = round(total_rank / i)
    return mean_rank


# 近三年加权录取最低排名平均值(全国一分一档表)
def assign_mean_rank(data):
    # 加权系数
    config = Config()
    assign_temp = config.getRowsByKey('assign')
    assign = dict()
    if assign_temp:
        for index, val in enumerate(assign_temp):
            assign[val['year']] = val['value']
    i = 0
    total_rank = 0
    if len(data) < 3:
        for index, row in enumerate(data):
            if row['lowest_rank'] > 0:
                i = i + 1
                total_rank = total_rank + row['lowest_rank']
    else:
        for index, row in enumerate(data):
            if index < 3 and row['lowest_rank'] > 0:
                i = i + 1
                total_rank = total_rank + row['lowest_rank'] * assign[row['year']]
    ass_mean_rank = 999999  # 默认设置加权平均排名
    if i > 0:
        ass_mean_rank = round(total_rank / i)
    return ass_mean_rank


# 近三年加权录取最低排名平均值(省内一分一档表)
def assign_lowest_rank(data, type):
    oneScoreRankShengNei = OneScoreRankShengNei()
    # 加权系数
    config = Config()
    assign_temp = config.getRowsByKey('assign')
    assign = dict()
    if assign_temp:
        for index, val in enumerate(assign_temp):
            assign[val['year']] = val['value']
    i = 0
    total_rank = 0
    if len(data) < 3:
        for index, row in enumerate(data):
            if row['lowest_score'] > 0:
                i = i + 1
                # 根据分数获取省内一分一档表获取排名
                one_score_rank = oneScoreRankShengNei.getOneByScore(row['lowest_score'], row['year'], type)
                if one_score_rank:
                    total_rank = total_rank + one_score_rank['rank']
    else:
        for index, row in enumerate(data):
            if index < 3 and row['lowest_score'] > 0:
                i = i + 1
                # 根据分数获取省内一分一档表获取排名
                one_score_rank = oneScoreRankShengNei.getOneByScore(row['lowest_score'], row['year'], type)
                if one_score_rank:
                    total_rank = total_rank + one_score_rank['rank'] * assign[row['year']]
    ass_mean_rank = 999999  # 默认设置加权平均排名
    if i > 0:
        ass_mean_rank = round(total_rank / i)
    return ass_mean_rank


def test():
    universityMajorRankMean = UniversityMajorRankMean()
    universityScoreRankMean = UniversityScoreRankMean()
    universityScoreRank = UniversityScoreRank()
    year = 2023
    mean_year = 2020  # 获取三年平均数时候的起始年份
    mean_year_2 = 2022  # 获取三年平均数时候的起始年份
    row = dict()
    row['code'] = '10589'
    total_list = list()
    row_1 = universityMajorRankMean.getOneByYearBatchType(year, 9, 2, row['code'])
    if row_1:
        # 院校排名
        university = University()
        university_row = university.getOneByCode(row['code'])
        if university_row:
            row_1['university_rank'] = university_row['university_rank']
            row_1['type_name'] = university_row['type_name']
            row_1['dual_class_name'] = university_row['dual_class_name']
            row_1['dual_class_A'] = university_row['dual_class_A']
            row_1['dual_class_B'] = university_row['dual_class_B']
            row_1['dual_class_subject'] = university_row['dual_class_subject']
            row_1['f985'] = university_row['f985']
            row_1['f211'] = university_row['f211']
            row_1['nature_name'] = university_row['nature_name']
            row_1['other_type'] = university_row['other_type']
            row_1['change_difficult_level'] = university_row['change_difficult_level']
        else:
            row_1['university_rank'] = 999  # 默认设置院校排名为999
            row_1['dual_class_name'] = ''
            row_1['dual_class_A'] = 0
            row_1['dual_class_B'] = 0
            row_1['dual_class_subject'] = 0
            row_1['f985'] = 0
            row_1['f211'] = 0
            row_1['type_name'] = ''
            row_1['nature_name'] = 0
            row_1['other_type'] = 0
            row_1['change_difficult_level'] = ''
        # 近三年录取最低分、录取最低排名、录取平均分、录取平均排名
        data_4 = universityScoreRank.getRowsByYearBatch(mean_year, mean_year_2, row_1['batch'], row_1['type'],
                                                        row_1['code'])

        # 计算专业近三年录取平均分的平均值、近三年录取最低分平均值、近三年录取最低排名平均值、近三年录取平均排名平均值
        row_1['mean_lowest_score'] = mean_lowest_score(data_4)
        row_1['mean_lowest_rank'] = mean_lowest_rank(data_4)
        row_1['mean_mean_score'] = mean_mean_score(data_4)
        row_1['mean_mean_rank'] = mean_mean_rank(data_4)
        row_1['assign_mean_rank'] = assign_mean_rank(data_4)
        row_1['assign_lowest_rank'] = assign_lowest_rank(data_4, row_1['type'])

        # 取最新一年的《高考指南》的投档数据
        one = universityScoreRank.getOneByYearBatch(2022, row_1['batch'], row_1['type'], row_1['code'])
        if one:
            row_1['plan_number'] = one['plan_number']
            row_1['submit_number'] = one['submit_number']
            row_1['enter_number'] = one['enter_number']
        else:
            row_1['plan_number'] = 0
            row_1['submit_number'] = 0
            row_1['enter_number'] = 0

        row_1['name'] = row_1['uname']
        del row_1['uname']
        print('row_1', row_1)
        total_list.append(row_1)

    # universityScoreRankMean.toInsertBatch(total_list)

def test2():
    universityMajorRankMean = UniversityMajorRankMean()
    universityScoreRankMean = UniversityScoreRankMean()
    universityScoreRank = UniversityScoreRank()
    year = 2023
    data = universityMajorRankMean.getListByGroup(year)
    for index, row in enumerate(data):
        print(row)
        row_1 = universityMajorRankMean.getOneByYearBatchType(year, 9, 1, row['code'])


# 更新最低分排位、平均分排位（取位次，不取累计人数）（广西院校按省内一分一档表，其它院校按全国一分一档表）
def toUpdateRank():
    universityScoreRank = UniversityScoreRank()
    oneScoreRank = OneScoreRank()
    oneScoreRankShengNei = OneScoreRankShengNei()
    for year in (2020, 2021, 2022):
        print(year)
        data = universityScoreRank.getList(year)
        for row in data:
            print(row)
            update_data = dict()
            if row['province_code'] == 450000:
                if row['lowest_score']:
                    lowest_rank_res = oneScoreRankShengNei.getOneByScore(row['lowest_score'], year, row['type'])
                    if lowest_rank_res:
                        update_data['lowest_rank'] = lowest_rank_res['rank']
                if row['mean_score']:
                    mean_rank_res = oneScoreRankShengNei.getOneByScore(row['mean_score'], year, row['type'])
                    if mean_rank_res:
                        update_data['mean_rank'] = mean_rank_res['rank']
                if update_data:
                    universityScoreRank.toUpdateRank(update_data, row['id'])
            else:
                if row['lowest_score']:
                    lowest_rank_res = oneScoreRank.getOneByScore(row['lowest_score'], year, row['type'])
                    if lowest_rank_res:
                        update_data['lowest_rank'] = lowest_rank_res['rank']
                if row['mean_score']:
                    mean_rank_res = oneScoreRank.getOneByScore(row['mean_score'], year, row['type'])
                    if mean_rank_res:
                        update_data['mean_rank'] = mean_rank_res['rank']
                if update_data:
                    universityScoreRank.toUpdateRank(update_data, row['id'])


if __name__ == "__main__":
    test()
